package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.github.gzuliyujiang.wheelview.contract.OnWheelChangedListener
import com.github.gzuliyujiang.wheelview.widget.WheelView
import com.lv.common.im.OnPositionMoreCancle
import com.lv.service.R
import com.lv.service.room.address.ReceiptAddressItem
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class CardCategoryTop4PopupView(
    private val mContext: Context,
    private val mAllBeans: List<ReceiptAddressItem>?,
    private var mWheelPosition1: Int = 0,
    private var mWheelPosition2: Int = 0,
    private var mWheelPosition3: Int = 0,
    private var mWheelPosition4: Int = 0,
    private var mWheelPos: Int? = -1,//显示第几个 -1 全部显示
    private val positionListener: OnPositionMoreCancle<String>? = null,
    private var mCancelText: String = "重置",
) :
    PartShadowPopupView(mContext) {
    private var tvConfirm: TextView? = null
    private var tvCancel: TextView? = null
    private var wheelLinkagePorvince: WheelView? = null
    private var wheelLinkageCity: WheelView? = null
    private var wheelLinkageArea: WheelView? = null
    private var wheelLinkageTown: WheelView? = null
    private var mDismissTag: Int = 0//0 确认 取消关闭  1 背景关闭
    private var viewBottom: View? = null

    override fun getImplLayoutId(): Int {
        return R.layout.card_four_top_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewBottom = findViewById<View>(R.id.view_bottom)
        tvConfirm = findViewById<TextView>(R.id.tv_confirm)
        wheelLinkagePorvince = findViewById<WheelView>(R.id.wheel_linkage_province)
        wheelLinkageCity = findViewById<WheelView>(R.id.wheel_linkage_city)
        wheelLinkageArea = findViewById<WheelView>(R.id.wheel_linkage_area)
        wheelLinkageTown = findViewById<WheelView>(R.id.wheel_linkage_town)
        tvCancel = findViewById<TextView>(R.id.tv_cancel)
        tvCancel?.text = mCancelText
        initView()
        initEvnet()
    }

    private fun initView() {


        when (mWheelPos) {
            0 -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.GONE
                wheelLinkageArea?.visibility = View.GONE
                wheelLinkageTown?.visibility = View.GONE
                wheelLinkagePorvince?.data = mAllBeans
                initWheel()

            }
            1 -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.VISIBLE
                wheelLinkageArea?.visibility = View.GONE
                wheelLinkageTown?.visibility = View.GONE
            }
            2 -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.VISIBLE
                wheelLinkageArea?.visibility = View.VISIBLE
                wheelLinkageTown?.visibility = View.GONE
            }
            else -> {
                wheelLinkagePorvince?.visibility = View.VISIBLE
                wheelLinkageCity?.visibility = View.VISIBLE
                wheelLinkageArea?.visibility = View.VISIBLE
                wheelLinkageTown?.visibility = View.VISIBLE
                initWheel()

            }

        }


    }

    private fun initWheel() {
        wheelLinkagePorvince?.data = mAllBeans
        wheelLinkagePorvince?.setDefaultPosition(mWheelPosition1)
        wheelLinkageCity?.data = mAllBeans?.get(mWheelPosition1)?.telSecondCategories
        wheelLinkageCity?.setDefaultPosition(mWheelPosition2)

        wheelLinkageArea?.data =
            mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories
        wheelLinkageArea?.setDefaultPosition(mWheelPosition3)

        wheelLinkageTown?.data =
            mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(
                mWheelPosition3
            )?.telFourCategories
        wheelLinkageTown?.setDefaultPosition(mWheelPosition4)

    }

    private fun initEvnet() {

        tvConfirm?.setOnClickListener {
//            val item = "${mAllBeans!!?.get(mWheelPosition1)?.name}-${mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.name}-${mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(mWheelPosition3)?.name}"
            var item: String? = null
            if (mWheelPos == -1) {
                item =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(
                        mWheelPosition3
                    )?.telFourCategories!![mWheelPosition4].name
            }
            positionListener?.onConfirmClick(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
                mWheelPosition4,
                data = item
            )
            mDismissTag = 0
            dismiss()
        }
        tvCancel?.setOnClickListener {
            mDismissTag = 1
            dismiss()
        }
        viewBottom?.setOnClickListener {
            mDismissTag = 0
            dismiss()
        }

        wheelLinkagePorvince?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {
                LogUtils.e("onWheelScrolled")
            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                LogUtils.e("onWheelScrolled==")

                mWheelPosition1 = position
                mWheelPosition2 = 0
                mWheelPosition3 = 0
                mWheelPosition4 = 0
                wheelLinkageCity?.data = mAllBeans?.get(mWheelPosition1)?.telSecondCategories
                wheelLinkageArea?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories
                wheelLinkageTown?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(
                        mWheelPosition3
                    )?.telFourCategories

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                LogUtils.e("onWheelScrollStateChanged")

            }

            override fun onWheelLoopFinished(view: WheelView?) {
                LogUtils.e("onWheelLoopFinished")

            }
        })
        wheelLinkageCity?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {

            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                mWheelPosition2 = position
                mWheelPosition3 = 0
                mWheelPosition4 = 0
                wheelLinkageArea?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories
                wheelLinkageTown?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(
                        mWheelPosition3
                    )?.telFourCategories

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
            }

            override fun onWheelLoopFinished(view: WheelView?) {
            }
        })
        wheelLinkageArea?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {

            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                mWheelPosition3 = position
                mWheelPosition4 = 0
                wheelLinkageTown?.data =
                    mAllBeans?.get(mWheelPosition1)?.telSecondCategories?.get(mWheelPosition2)?.telThirdCategories?.get(
                        mWheelPosition3
                    )?.telFourCategories

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
            }

            override fun onWheelLoopFinished(view: WheelView?) {
            }
        })
        wheelLinkageTown?.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onWheelScrolled(view: WheelView?, offset: Int) {

            }

            override fun onWheelSelected(view: WheelView?, position: Int) {
                mWheelPosition4 = position

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
            }

            override fun onWheelLoopFinished(view: WheelView?) {
            }
        })

    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()
        positionListener?.onConcelClick(mDismissTag)

    }


}